package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.apache.commons.codec.digest.DigestUtils;


import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

/**
 * 员工管理模块
 */
@RestController
@RequestMapping("employee")
@Api(tags = "员工模块")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登录
     *
     * @param employee
     * @param session
     * @return
     */
    @PostMapping("login")
    @ApiOperation("员工登入")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session) {
        String username = employee.getUsername();
        String password = employee.getPassword();
        //根据用户名查询
        Employee e = employeeService.lambdaQuery()
                .eq(Employee::getUsername, username)
                .one();
        if (e == null) {
            //说明账号不存在
            return R.error("账号不存在");
        }
        //判断密码是否一致
        if (!StringUtils.equals(e.getPassword(), DigestUtils.md5Hex(password.getBytes()))) {
            //说明密码错误
            return R.error("密码错误");//返回错误结果集
        }

        //判断用户状态
        if (e.getStatus() == 0) {
            //说明状态禁用
            return R.error("账号已经禁用");
        }
        session.setAttribute("employee", e.getId());//
        return R.success(e);
    }

    /**
     * 员工退出登入
     *
     * @param session
     * @return
     */
    @PostMapping("logout")
    public R<String> logout(HttpSession session) {
        //删除session中的用户信息,销毁用户信息
        //session.removeAttribute("employee");
        session.invalidate();
        //返回成功标识code=1
        return R.success("退出成功");
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee, HttpSession session) {
        //补全信息
      /*  employee.setPassword(DigestUtils.md5Hex("123456"));
        employee.setCreateTime(LocalDateTime.now());//新增时间
        employee.setCreateTime(LocalDateTime.now());//修改时间*/
        //操作员是
        Long empId = (Long) session.getAttribute("employee");//从session中获取员工id
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        employeeService.save(employee);
        return R.success("新增成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */

    @GetMapping("page")
    public R<Page<Employee>> page(int page, int pageSize, String name) {
        //创建分页对象
        Page<Employee> employeePage = new Page<>(page, pageSize);
        //分页查询
        employeeService.query()
                .like(StringUtils.isNotBlank(name),"name",name)
                .orderByDesc("update_time")
                .page(employeePage);
       /* employeeService.lambdaQuery()
                .like(StringUtils.isNotBlank(name), Employee::getName, name)
                .orderByDesc(Employee::getUpdateTime)
                .page(employeePage);*/
        return R.success(employeePage);
    }

    /**
     * 修改
     * @param employee
     * @param session
     * @return
     */
    @PutMapping
    public R<String> update( @RequestBody Employee employee,HttpSession session){
       //补全修改信息(修改人,修改时间)
        //employee.setUpdateTime(LocalDateTime.now());//修改时间
        Long employeeId = (Long) session.getAttribute("employee");//重session中获取员工id
        employee.setUpdateUser(employeeId);//设置操作人

        //调用业务成修改数据库
        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    /**
     * 根据id查询员工信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> findById(@PathVariable Long id ){
        Employee byId = employeeService.getById(id);
        return R.success(byId);
    }
}




